package util;

import java.security.MessageDigest;
import java.util.List;

import model.Usuario;

public class Criptografia {
	
	public String criptoPassword(String original) throws Exception {
		MessageDigest algorithm = MessageDigest.getInstance("SHA-256");
		byte messageDigest[] = algorithm.digest(original.getBytes("UTF-8"));

		StringBuilder hexString = new StringBuilder();
		for (byte b : messageDigest) {
			hexString.append(String.format("%02X", 0xFF & b));
		}
		String senha = hexString.toString();
		return senha;
	}
	
	public boolean validaSenha(String login, String senha,List<Usuario> usuarios) throws Exception{
		
		boolean result = false;
		
		for(Usuario u : usuarios){
			if(u.getLogin().equals(login)){
				if(u.getSenha().equals(criptoPassword(senha))){
					result = true;
				}
				else{
					result = false;
				}
			}
		}
		return result;
	}
}
